package copyengine.ui.component.progressbar
{
	import flash.display.DisplayObjectContainer;

	import copyengine.ui.component.CEUISprite;
	import copyengine.ui.component.progressbar.animation.ICEProgressBarAnimation;
	import copyengine.utils.GeneralUtils;

	public class CEProgressBar extends CEUISprite
	{
		private var animation:ICEProgressBarAnimation;
		private var progressPercent:Number=-1;
		private var onProgressPercentChangeCallback:Function;
		private var fiexedNum:int;

		public function CEProgressBar(_target:DisplayObjectContainer)
		{
			super(_target);
		}

		/**
		 * @param _onProgressPercentChangeCallback				当进度发生改变时候的回调函数
		 * @param _fixedNum														小数点后保留的位数,默认为0位 及百分比默认为整数
		 *
		 */
		public function initialize(_onProgressPercentChangeCallback:Function, _defaultPercent:int=0, _fixedNum:int=0):void
		{
			fiexedNum=_fixedNum;
			//注意 先调用set函数设置percent,进行初始化,然后再设置回调函数
			//这样回调函数不会被触发
			percent=_defaultPercent;
			onProgressPercentChangeCallback=_onProgressPercentChangeCallback;
		}

		public function setAnimation(_animation:ICEProgressBarAnimation):void
		{
			animation=_animation;
		}

		public function set percent(_value:Number):void
		{
			_value=GeneralUtils.normalizingValue(_value, 0, 100);
			if (_value.toFixed(fiexedNum) != progressPercent.toFixed(fiexedNum))
			{
				progressPercent=_value;
				animation && animation.setProgressPercent(progressPercent);
				onProgressPercentChangeCallback && onProgressPercentChangeCallback(Number(progressPercent.toFixed(fiexedNum)))
			}
		}

		public function get percent():Number
		{
			return Number(progressPercent.toFixed(fiexedNum));
		}

		override protected function doDispose():void
		{
			animation && animation.dispose();
			animation=null;
			onProgressPercentChangeCallback=null;
		}

	}
}
